package ma.platform.server.core.util;

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Copyright:
 * Author:
 * Description:
 * Created by 李林山 on 2024/3/28
 */
@Slf4j
public class BaseConvert {

    public static <T, S> S convert(T source, Class<S> clazz) {

        if (null == source) {
            return null;
        }
        S target = null;
        try {
            target = clazz.newInstance();
            BeanUtils.copyProperties(source, target);
        } catch (Exception e) {
//            log.error("BaseConverter convert error, t = {}, class = {}", source, clazz, e);
        }
        return target;
    }

    public static <T, S> List<S> convertList(List<T> t, Class<S> clazz) {
        if (CollectionUtils.isEmpty(t)) {
            return Lists.newArrayList();
        }
        return t.stream().map(o -> convert(o, clazz)).collect(Collectors.toList());
    }

    public static <T, S> List<S> convertList(List<T> t, Class<S> clazz, String[] ignoreProperties) {
        if (CollectionUtils.isEmpty(t)) {
            return Lists.newArrayList();
        }
        return t.stream().map(o -> convert(o, clazz)).collect(Collectors.toList());
    }

    public static <T, S> List<S> convertList(List<T> t, Function<T, S> function) {
        if (CollectionUtils.isEmpty(t)) {
            return Lists.newArrayList();
        }
        return t.stream().map(function::apply).collect(Collectors.toList());
    }

}
